﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class VendorPODetail : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["vendPOId"] != null)
                    {
                        int vendPOId = Convert.ToInt32(Request.QueryString["vendPOId"]);
                        VendorPOController vendorPoController = new VendorPOController();
                        VendorPOEntity vendorPoEntity = vendorPoController.findVendorPOById(vendPOId);
                        if(vendorPoEntity.Status!="new")
                        {
                            //EditButton.Enabled = false;
                            DeleteButton.Enabled = false;
                        }
                        VendorPOItemController vendorPoItemController = new VendorPOItemController();
                        IList<VendorPOItemEntity> vendorPoItemEntities =
                            vendorPoItemController.findVendorPOItemsByVendorPOId(vendPOId);
                        VendorPOitemGridView.DataSource = vendorPoItemEntities;
                        VendorPOitemGridView.DataBind();

                        //Bind the vendor po info.
                        VendorPONoLabel.Text = vendorPoEntity.No;
                        POTypeLabel.Text = vendorPoEntity.Type;
                        CreateDateLabel.Text = vendorPoEntity.Date.ToString("d MMM, yyyy");
                        VendorLabel.Text = vendorPoEntity.VendorShortName;
                        POStatusLabel.Text = vendorPoEntity.Status;
                        AddressLabel.Text = vendorPoEntity.Address;
                        ZipCodeLabel.Text = vendorPoEntity.ZipCode;
                        AttentionLabel.Text = vendorPoEntity.Attention;
                        PhoneLabel.Text = vendorPoEntity.Phone;
                        FaxLabel.Text = vendorPoEntity.Fax;
                        CreatedByLabel.Text = vendorPoEntity.CreatedBy.ToString();
                        ApprovedByLabel.Text = vendorPoEntity.ApprovedBy.ToString();
                        RefereceLabel.Text = vendorPoEntity.Reference;
                        RemarkLabel.Text = vendorPoEntity.Remark;
                        TotalQtyLabel.Text = vendorPoEntity.TotalQty.ToString();

                        SummaryPOTypeLabel.Text = vendorPoEntity.Type;
                        SummaryTotalOpenQtyLabel.Text = vendorPoEntity.TotalOpenQty.ToString();
                    }
                    else
                    {
                        Response.Redirect("Default.aspx",false);
                    }

                }
                
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect("Default.aspx",false);
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                VendorPOController vendorPoController = new VendorPOController();
                int vendPOId = Convert.ToInt32(Request.QueryString["vendPOId"]);
                vendorPoController.DeleteVendorPOById(vendPOId);
                Session["FlashMessage"] = "Success: You Have Deleted That Order And Its Order Items!";
                Session["FlashMessageType"] = "success";
                Response.Redirect("~/VendorPOList.aspx", false);
            }
            catch(Exception ex)
            {
                Session["FlashMessage"] = "Error: " + ex.Message;
                Session["FlashMessageType"] = "error";
            }

        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            int vendPOId = Convert.ToInt32(Request.QueryString["vendPOId"]);
            Response.Redirect("EditVendorPO.aspx?vendorPOId=" +vendPOId);
        }

        protected void GenerateExcelButton_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["vendPOId"] != null)
            {
                int vendorPOId = Convert.ToInt32(Request.QueryString["vendPOId"]);
                string filename = ExcelGenerator.GenerateExcelForVendorPOById(vendorPOId);
                Response.Redirect("~/"+filename);
            }
            else
            {
                Response.Redirect("~/VendorPOList.aspx");
            }
        }
    }
}